package com.schoolET.service.wechat.wechatImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolET.entity.Region;
import com.schoolET.entity.Wechat;
import com.schoolET.entity.WechatAndRegion;
import com.schoolET.entity.dto.WechatAndRegionDTO;
import com.schoolET.error.costom.ChangeRegionException;
import com.schoolET.error.costom.NoWechatRegionException;
import com.schoolET.error.costom.RepeatedJoinRegionException;
import com.schoolET.mapper.WechatAndRegionMapper;
import com.schoolET.service.admin.IRegionService;
import com.schoolET.service.admin.IWechatService;
import com.schoolET.service.wechat.IWechatAndRegionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 微信用户与区域关联表 服务实现类
 * </p>
 *
 * @author le
 * @since 2024-06-06
 */
@Service
public class WechatAndRegionServiceImpl extends ServiceImpl<WechatAndRegionMapper, WechatAndRegion> implements IWechatAndRegionService {
    @Resource
    private IWechatService wechatService;
    @Resource
    private IRegionService regionService;


    @Override
    public boolean addWechatAndRegion(Integer code, String openId) {
        //查询用户是否加入该区域
        Wechat wechat = wechatService.selectWechatByOpenId(openId);
        Region region = regionService.selectRegionByCode(code);
        Integer wechatId = wechat.getId();
        Integer regionId = region.getId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("wechat_id",wechatId);
        queryWrapper.eq("region_id",regionId);
        if (baseMapper.selectOne(queryWrapper) != null){
            throw new RepeatedJoinRegionException();
        }
        //查询用户是否有默认区域
        QueryWrapper<WechatAndRegion> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("wechat_id",wechatId);
        queryWrapper1.eq("default_location",1);
        WechatAndRegion wechatAndRegion1 = baseMapper.selectOne(queryWrapper1);
        //如果有改为非默认区域
        if (wechatAndRegion1 != null) {
            baseMapper.updateDefaultLocationById(wechatAndRegion1.getId());
        }
        //将加入区域设置为默认区域
        WechatAndRegion wechatAndRegion = new WechatAndRegion();
        wechatAndRegion.setWechatId(wechatId);
        wechatAndRegion.setRegionId(regionId);
        wechatAndRegion.setDefaultLocation(1);
        int insert = baseMapper.insert(wechatAndRegion);
        if (insert > 0){
            return true;
        }
        return false;
    }

    @Override
    public List<WechatAndRegionDTO> regionList(Integer wechatId) {
        QueryWrapper<WechatAndRegion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wechat_id",wechatId);
        return baseMapper.selectWechatRegionList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean updateDefaultLocation(Integer regionId, String openId) {
        WechatAndRegion wechatAndRegion = baseMapper.checkWechatRegion(regionId, openId);
        if (wechatAndRegion == null){
            throw new NoWechatRegionException();
        }
        if (wechatAndRegion.getDefaultLocation() ==1){
            throw new ChangeRegionException();
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("wechat_id",wechatAndRegion.getWechatId());
        updateWrapper.eq("default_location",1);
        updateWrapper.set("default_location",0);
        int update = baseMapper.update(updateWrapper);
        wechatAndRegion.setDefaultLocation(1);
        int updateById = baseMapper.updateById(wechatAndRegion);
        if (!(update > 0 && updateById > 0)){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

}
